#    This file is part of robo-ircbot.
#
#    robo-ircbot is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    robo-ircbot is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with robo-ircbot.  If not, see <http://www.gnu.org/licenses/>.

package App::Modular::Module::Kick;
use strict;
use warnings;
use base qw(App::Modular::Module);

print ":: Kick module loaded.\n";

# Events you would like to receive.
# (all scripts need this method)
sub events { return "KICK JOIN" }

sub help {
        "Returns an insult to the douche that kicked me."
}

# KICK events.
sub event_kick
{
        shift;
        my $e = shift;

        my $help = $e->robo->mod("Helper") || return;
        my $nick = $help->parse_nick($e->from);

        # See if I am being kicked.
        if ($e->knick eq $e->cfg->nick) {
                $e->cfg->set_mod("kick/".$e->to, $nick);
                $e->s("JOIN ".$e->to);
        }
}

# JOIN events.
sub event_join
{
        shift;
        my $e = shift;

        my $help = $e->robo->mod("Helper") || return;
        my $nick = $help->parse_nick($e->from);

        # See if I am joining.
        if ($nick eq $e->cfg->nick) {
                # See if I was kicked from this channel.
                if ($e->cfg->get_mod("kick/".$e->to)) {
                        # Array of insults.
                        my @insults = (
                                # Tribute to sdbot.
                                'screw you %nick%!',
                                'you know what %nick%? EFF YOU.',
                                'wow %nick%, really?  suck a nub.',
                                'watch your back %nick%, watch it good.',
                                '%nick%: may you plz die of aids.',
                                'hey my name is %nick% and i am an effin turd!',
                        );

                        # The loser that kicked me.
                        my $loser = $e->cfg->get_mod("kick/".$e->to);

                        # Grab random insult and replace %nick% and %chan% with data.
                        my $to = $e->to;
                        my $insult = $insults[int rand($#insults)];
                        $insult =~ s/\%nick\%/$loser/g;
                        $insult =~ s/\%chan\%/$to/g;

                        # Send message to channel.
                        $e->s("PRIVMSG ".$e->to." :".$insult);

                        # Delete the kick chan + nick from mod config.
                        $e->cfg->del_mod("kick/".$e->to);
                }
        }
}

1;
